package com.genfu.reform.jpa;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.LockModeType;

public class GenfuCommonTransaction implements GenfuCommonDAO {

	public EntityManagerFactory entityManagerFactory;

	public EntityManagerFactory getEntityManagerFactory() {
		return entityManagerFactory;
	}

	public void setEntityManagerFactory(
			EntityManagerFactory entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}

	@Override
	public <T> List<T> searchList(String jpql, Map<String, Object> parameters,
			Class<T> entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> int getTotalRecords(String jpql, Map<String, Object> parameters,
			Class<T> entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public <T> Object find(Long id, Class<T> entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int save(Object model) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int merge(Object model) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int remove(Object model) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int remove(long id, String tableName) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public <T> Object remove(List<T> list) {
		EntityManager em = entityManagerFactory.createEntityManager();
		EntityTransaction entityTransaction = em.getTransaction();
		entityTransaction.begin();
		for (Object object : list) {
			em.remove(object);
		}
		em.clear();
		em.close();
		entityTransaction.commit();

		return 0;
	}

	@Override
	public <T> Object merge(List<T> list) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object find(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T findModel(Long id, Class<T> entity) {

		return null;
	}

	@Override
	public int batchDelete(String strSQL) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public <T> int batchDeleteJPQL(String strSQL, Class<T> entity) {
		EntityManager em = entityManagerFactory.createEntityManager();
		EntityTransaction entityTransaction = em.getTransaction();
		entityTransaction.begin();
		List<T> resultList = em.createQuery(strSQL, entity).getResultList();

		for (Object object : resultList) {
			em.remove(object);
		}
		entityTransaction.commit();
		em.clear();

		return 0;
	}

	@Override
	public <T> List<T> searchModel(String jpql, Map<String, Object> parameters,
			Class<T> entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T find(Class<T> entityClass, Object primaryKey,
			LockModeType lockMode, Map<String, Object> properties) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T find(Class<T> entityClass, Object primaryKey,
			LockModeType lockMode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T find(Class<T> entityClass, Object primaryKey,
			Map<String, Object> properties) {
		// TODO Auto-generated method stub
		return null;
	}

}
